home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: overloading new operator
- Message-ID: <marnoldDn0A8r.Ipu@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <4g8air$6a0@newsbf02.news.aol.com>
- Date: Mon, 19 Feb 1996 04:38:51 GMT
- Sender: marnold@netcom20.netcom.com
-
- cperkins@aol.com (C Perkins) writes:
-
- >Does anyone have a simple sample of overriding the new operator? How do I
- >use new placement with constructors that take multiple arguments?
-
- Placement new (or any form of overloaded new) has nothing do with
- how many arguments constructors have.
-
- >I have a number of classes which have constructors that take multiple
- >arguments. Is it possible to dynamically create objects of these classes
- >using placement? Can anyone show me an example?
-
- Are you clear on what "placement new" is? I'll admit some C++ books
- call overloaded new operators "placement" new, but there is really
- only one classic form of placement new. I'll try to clear this up for
- you...
-
- Placement new usually refers to the form of operator new that takes
- a pointer to some "place" in memory and then simply returns that pointer.
- It doesn't actually perform any memory allocation. It is typically
- defined like so...
-
- void* operator new(size_t size, void* p)
- {
- return p;
- }
-
- If your compiler is farily up to date, you'll find the declaration
- for this form of operator new in the header file <new.h>. Because
- this form of new lets you explicity place an object in memory, it is
- known as "placement new".
-
- This form allows you to write code like this...
-
- char my_memory[100];
-
- new(my_memory) MyObject;
-
- ...which explicitly *places* the newed instance of MyObject into
- the array of bytes called my_memory. This kind of fine control
- over object construction is usually required in only the *rarest*
- of circumstances and must be used carefully.
-
- So, the mechanics of overloading operator new are simple. The size
- of the object getting newed is passed as the size_t parameter.
- Additional parameters are passed as the 2nd, 3rd, etc. parameters.
- In the above case, the address of my_memory arrives inside the
- operator new as the void* parameter. There can be any number of
- these extra parameters and the can have any type.
-
- So, the above example is what most people think of when you say
- "placement new". Other forms are usually referred to as simply
- "overloaded" new, but some C++ books calls all forms of new with
- "extra" parameters placement new, even the ARM, and books by B.
- Stroubstrup, the creator of C++. I think this is confusing, and
- I prefer to use the description "placement new" for only the
- "classic" form and the description "overloaded new" for all other
- fomrs. I hope you find that clearer too.
-
-
- Anyhow, from your other comments, it sounds like you are asking
- about *overloading* operator new to actually perform some kind of
- custom memory allocation, not about the classic form of placement
- new.
-
- For this, you need to simply provide you own global or class-
- specific operator new.
-
- Here is an example of a global operator new...
-
- void* operator new(size_t size)
- {
- return malloc(size);
- }
-
- All I've done here is make operator new simply use the standard
- C library routine malloc() to get memory. This, in essence, is
- what most standard operator new's do. To find out what your
- standard new does, examine your compiler's run-time library source
- code or ask your compiler vendor.
-
- NOTE: The real standard operator new does a few other things, like
- calling the new_handler() when memory is low, etc..
-
- Also, it is very important (in this example, anyway), to make sure
- global operator *delete* does the right thing in relation to new.
- With the above new, we'd probably also want to overload operator
- delete, like so...
-
- void operator delete(void* p)
- {
- free(p); // opposite of malloc()
- }
-
- You usually overload operator new and delete as a pair.
-
- If you had some special dynamic memory allocator you wanted to use,
- you could do this....
-
- void* operator new(size_t size)
- {
- return GetMemFromMySpecialAllocator(size);
- }
-
- ...and likewise define an operator delete to do the reverse.
-
- Basically, in a custom operator new, all you have to do is allocate
- size bytes and return the pointer to them. It has nothing to do
- with how many parameters an object's constructor takes. Operator
- new is not even aware of the type of object being newed. It's only
- task is to allocate memory.
-
- To use an overloaded version of global operator new, just use new
- like usual...
-
- Object* p = new Object;
-
- If Object's can be constructed with other parameters, just do...
-
- Object* p = new Object(1, 2, 3);
-
- ...it doesn't affect how new is called.
-
- I also mentioned class-specific operator new. You use this when
- you want to provide custom memory allocation *only* for a specific
- class of objects, not all objects, as you do with overloading
- global operator new.
-
- To do this, you simply define an operator new as a member of the
- specific class, like so...
-
- void* Object::operator new(size_t size)
- {
- // return pointer to memory to hold a Object instance
- }
-
- Now, if you do this...
-
- Object* p = new Object;
-
- ...Object::operator new() is called. But, if you do this...
-
- Foo* p = new Foo;
-
- ...the global operator new is called (unless, of course, Foo has
- it's own class-specific operator new). Object's class-specific
- operator new does not affect class Foo.
-
- >If I overload new with placement, is
- >my task simply to allocate the memory, and will the compiler then handle
- >calling the correct constructor?
-
- I think, after reading the above, you'll understand my answer when
- I just say "yes".
-
- >I've gone through nearly every book C++ I've got, and all mention
- >overloading new, but none have any examples.
-
- Which books do you have? Maybe we can recommend some better ones.
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-